﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using System.Resources;

namespace SkinEngines.SkinForm.Painting
{
    /// <summary>
    /// This wrapper contains all information for painting a component
    /// </summary>
    public class PaintHelperData
    {
        /// <summary>
        /// Gets or sets the image strip image used for painting.
        /// </summary>
        /// <value>The image strip image.</value>
        public Bitmap Image { get; set; }

        /// <summary>
        /// Gets or sets the size of one single image in the image strip.
        /// </summary>
        /// <value>The size of the image.</value>
        public Size ImageSize { get; set; }

        /// <summary>
        /// Gets or sets the padding indicating the border size.
        /// </summary>
        /// <value>The sides.</value>
        public Padding ImagePadding { get; set; }

        /// <summary>
        /// Gets or sets the bounds of the content section.
        /// </summary>
        /// <remarks>
        /// This placeholder is used later to calculate the content bounds. 
        /// </remarks>
        /// <value>The content bounds.</value>
        public Rectangle ContentBounds { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="PaintHelperData"/> class.
        /// </summary>
        public PaintHelperData()
        {
            ImagePadding = Padding.Empty;
        }

        public static PaintHelperData Read(XmlNode node, ResourceManager resManager, string imageName)
        {
            PaintHelperData data = new PaintHelperData
            {
                Image = (Bitmap)resManager.GetObject(imageName)
            };

            XmlNode child = node.FirstChild;
            while (child != null)
            {
                switch (child.Name)
                {
                    case "ImageSize":
                        data.ImageSize = StringToSize(child.InnerText);
                        break;
                    case "ImagePadding":
                        data.ImagePadding = StringToPadding(child.InnerText);
                        break;
                }
                child = child.NextSibling;
            }
            return data;
        }

        public static Size StringToSize(string value)
        {
            string[] split = value.Split(',');
            if (split.Length != 2)
                throw new ApplicationException("Invalid Value for Size");

            return new Size(int.Parse(split[0]), int.Parse(split[1]));
        }

        private static Padding StringToPadding(string value)
        {
            string[] split = value.Split(',');
            if (split.Length != 4)
                throw new ApplicationException("Invalid Value for Padding");

            return new Padding(int.Parse(split[0]), int.Parse(split[1]), int.Parse(split[2]), int.Parse(split[3]));
        }

        public static Color StringToColor(string value)
        {
            string[] split = value.Split(',');
            if (split.Length != 3)
                throw new ApplicationException("Invalid Value for Color");

            return Color.FromArgb(int.Parse(split[0]), int.Parse(split[1]), int.Parse(split[2]));
        }

        public static bool StringToBool(string value)
        {
            return bool.Parse(value);
        }
    }
}
